package com.laxcen.toydemo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.laxcen.controls.HomeMenuButtonControl;
import com.laxcen.intelligenttoy.BLE.ETag;
import com.laxcen.lib.GameFailedDialog;
import com.laxcen.lib.GameSuccessDialog;

import util.BackgroundMusic;
import util.DefaultHandler;
import util.DelayHandler;
import util.RandomUtil;
import util.SoundClass;

public class GameStep5Activity extends BaseActivity implements View.OnClickListener {
    GameState mGameState = GameState.instance(this);
    HomeMenuButtonControl btn_home;
    HomeMenuButtonControl btn_option1, btn_option6;
    HomeMenuButtonControl btn_daxiao, btn_daku;
    HomeMenuButtonControl img_xiaoxiong_eat_apple;
    HomeMenuButtonControl btn_food1, btn_food2, btn_food3, btn_food4, btn_food5, btn_food6;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_step5);
        mGameState.start();

        btn_home = findViewById(R.id.btn_home);
        btn_home.setOnClickListener(this);

        btn_option1 = findViewById(R.id.btn_option1);
        btn_option1.setOnClickListener(this);

        btn_option6 = findViewById(R.id.btn_option6);
        btn_option6.setOnClickListener(this);

        btn_daxiao = findViewById(R.id.btn_daxiao);
        btn_daxiao.setOnClickListener(this);
        btn_daku = findViewById(R.id.btn_daku);
        btn_daku.setOnClickListener(this);

        btn_food1 = findViewById(R.id.btn_food1);
        btn_food2 = findViewById(R.id.btn_food2);
        btn_food3 = findViewById(R.id.btn_food3);
        btn_food4 = findViewById(R.id.btn_food4);
        btn_food5 = findViewById(R.id.btn_food5);
        btn_food6 = findViewById(R.id.btn_food6);


        img_xiaoxiong_eat_apple = findViewById(R.id.img_xiaoxiong_eat_apple);
        hideAllFood();
    }

    private void startRepeatPromptGbSound() {
        new DefaultHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mGameState.getStep() == GameState.EGameStep.GameStep5) {
                    BackgroundMusic.instance(GameStep5Activity.this).play(R.raw.audio_game_step5_how_many_eat, false);
                }
            }
        }, 2000);
    }

    private void hideAllFood() {
        btn_food1.hide();
        btn_food2.hide();
        btn_food3.hide();
        btn_food4.hide();
        btn_food5.hide();
        btn_food6.hide();
    }

    private void showAllFood() {
        int delay = 300;
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                btn_food1.show();
                btn_food1.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_food2.show();
                btn_food2.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_food3.show();
                btn_food3.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_food6.show();
                btn_food6.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_food5.show();
                btn_food5.shake();
            }
        }, delay).delay(new Runnable() {
            @Override
            public void run() {
                btn_food4.show();
                btn_food4.shake();
            }
        }, delay);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_apple1 || v.getId() == R.id.btn_apple2) {
            HomeMenuButtonControl btn_apple = (HomeMenuButtonControl) v;
//            btn_apple.shake();
        } else if (v.getId() == R.id.btn_home) {
            goback();
        } else if (v.getId() == R.id.btn_option1) {
            onOption1Active();
        } else if (v.getId() == R.id.btn_option6) {
            onOption6Active();
        }
    }

    private void goback() {
        isWaitTag = false;
        BackgroundMusic.instance(this).stop();
        startActivityAndFinish(GameStepSelectActivity.class);
    }

    private void onOption6Active() {
//        btn_apple2.shake();

        onRightOptionActive();
    }

    private void onOption1Active() {
        hideAllFood();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                btn_daku.show();
                btn_daku.bigSplash();
                new SoundClass(GameStep5Activity.this).playSound(R.raw.audio_daku);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep5Activity.this).playSound(R.raw.audio_game_step5_error_prompt);
            }
        }, 3000).delay(new Runnable() {
            @Override
            public void run() {
                showFalied();
            }
        }, 7000);
    }

    private void onRightOptionActive() {
        hideAllFood();
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                btn_daxiao.show();
                btn_daxiao.bigSplash();
                new SoundClass(GameStep5Activity.this).playSound(R.raw.audio_daxiao);

            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep5Activity.this).playSound(R.raw.audio_game_step5_success_prompt);
            }
        }, 3000).delay(new Runnable() {
            @Override
            public void run() {
                showAllFood();
            }
        }, 8000).delay(new Runnable() {
            @Override
            public void run() {
                restartTagCheck();
            }
        }, 2000).delay(new Runnable() {
            @Override
            public void run() {
                new SoundClass(GameStep5Activity.this).playSound(R.raw.audio_game_last_msg);
            }
        }, 1000).delay(new Runnable() {
            @Override
            public void run() {
                showSuccess();
            }
        }, 4000);

    }

    private void showSuccess() {
        GameSuccessDialog successDialog = GameSuccessDialog.getInstance(this);
        successDialog.show("", RandomUtil.generateOneNumber(0, 5));
        successDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        successDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
    }

    private void showFalied() {
        GameFailedDialog failedDialog = GameFailedDialog.getInstance(this);
        failedDialog.show();
        failedDialog.setGobackClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
        failedDialog.setGoNextClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                startActivity(GameStep5Activity.class);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        BackgroundMusic.instance(this).stop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGameState.setStep(GameState.EGameStep.GameStep5);
        startRepeatPromptGbSound();
        restartTagCheck();
    }


    private boolean isWaitTag = true;

    private void restartTagCheck() {
        DelayHandler.newHandler().delay(new Runnable() {
            @Override
            public void run() {
                isWaitTag = true;
                startTagCheck();
            }
        }, 1000);
    }

    /* 7,8,9 = 1,2,3 */
    private void startTagCheck() {
        if (!isWaitTag) return;
        if (checkOption(currentTags, ETag._6)) {
            isWaitTag = false;
            onOption6Active();
        } else if (checkOption(currentTags, ETag._GOBACK)) {
            isWaitTag = false;
            goback();
        } else if (checkHasAnyTag(currentTags)) {
            isWaitTag = false;
            onOption1Active();
        }

        if (isWaitTag) {
            DelayHandler.newHandler().delay(new Runnable() {
                @Override
                public void run() {
                    startTagCheck();
                }
            }, LoopDetectionMillis);
        }
    }
}
